/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package business;

import data.Grouptable;
import data.Userinfotable;
import data.Usertable;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

/**
 *
 * @author stana
 */
@SessionScoped
@ManagedBean
public class NewUserBean {
    @Size(min=5,message="minimálně 5 znaků")
    private String name;
    @Size(min=5,message="minimálně 5 znaků")
    private String password;
    private String secondPassword;
    private String code;
    private int validCode=-1;//kod ktery je ted validni
    @Size(min = 9, max = 9, message = "číslo musí mít 9 znaků")
    @Pattern(regexp = "[0-9]+", message = "číslo musí obsahovat pouze numericlé znaky")
    private String phoneNumber;
    private Grouptable group;
    private Usertable user;
    private Userinfotable userInfo;
    private String info;
    private String validNumber;//cislo pro ktere byl zaslan kod
    @EJB
    private GrouptableFacade groupFacade;
    @EJB
    private UsertableFacade userFacade;
    @EJB
    private UserinfotableFacade userInfoFacade;

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public String getPhoneNumber()
    {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber)
    {
        this.phoneNumber = phoneNumber;
    }

    public String getInfo()
    {
        return info;
    }

    public void setInfo(String info)
    {
        this.info = info;
    }

    public String getSecondPassword()
    {
        return secondPassword;
    }

    public void setSecondPassword(String secondPassword)
    {
        this.secondPassword = secondPassword;
    }

    public String getCode()
    {
        return code;
    }

    public void setCode(String code)
    {
        this.code = code;
    }





    public String createUser()
    {
        if(code==null||code.isEmpty()||validCode!=Integer.parseInt(code))
        {
            info="Zadali jste chybný validační kód.";
            code="";
            return"";
        }
        if(!validNumber.equals(phoneNumber))
        {
            info="Zadali jste chybný validační kód.";
            code="";
            return"";
        }
        if(!password.equals(secondPassword))
        {
            info="Kontrola hesla nesouhlasí.";
            password="";
            secondPassword="";
            return "";
        }
        String pswd=null;
        try
        {
            pswd = MD5Algorithm.getMD5(password);
        } catch (NoSuchAlgorithmException ex)
        {
            Logger.getLogger(NewUserBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex)
        {
            Logger.getLogger(NewUserBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        user=new Usertable(getName(), pswd);

        try
        {
            userFacade.create(user);
        }
        catch(Exception e)
        {
            name="";
            info="Jméno je již použité zvolte jiné.";
            return "";
        }
        userInfo =new Userinfotable();
        userInfo.setUserid(getName());
        userInfo.setPhoneNumber(phoneNumber);
        group=new Grouptable(name, "USERS");
        try
        {
            userInfoFacade.create(userInfo);
            
        }
        catch(Exception e)
        {
            info="Uživatel s Vaším číslem již existuje. Účet nelze vytvořit.";
            userFacade.remove(user);
            return "";
        }
        try
        {
            groupFacade.create(group);
        }
        catch(Exception e)
        {
            userFacade.remove(user);
            userInfoFacade.remove(userInfo);
        }
        cleanValues();
        return "/LoginSuccess";
    }
    private void cleanValues()
    {
        info="";
        validCode=-1;
        validNumber=null;
        name="";
        phoneNumber="";
        password="";
        secondPassword="";
        code="";
    }

    public void sendCode()
    {
        info="";
        validNumber=phoneNumber;
        validCode=CodeGenerator.sendCode(phoneNumber);
    }


    
  
    
}
